/******************************************************************************
 *  _____     _____
 *  \  __\    \  __\
 *   \ \_\\    \ \_\\
 *    \  __\SS  \  __\roject
 *     \_\       \_\
 *
 * Mateusz Maciąg, Rafał Urbański
 * Komputerowe Systemy Sterowania
 *****************************************************************************/

#include "InputStepDesigner.h"
#include "ui_InputStepDesigner.h"

#include <QMessageBox>

using namespace std;

InputStepDesigner::InputStepDesigner(QWidget *parent) :
    InputDesigner(parent),
    ui(new Ui::InputStepDesigner)
{
    ui->setupUi(this);
}

InputStepDesigner::~InputStepDesigner()
{
    delete ui;
}

void InputStepDesigner::attachInput(RafMat::SimInterface::IConfigurableSPtr ctrl)
{
    m_inputConf = ctrl;

    try
    {
        string initVal = m_inputConf->getParameter("init_val");
        string stepTime = m_inputConf->getParameter("action_time");
        string stepVal = m_inputConf->getParameter("action_val");

        ui->cfgInitial->setValue(QString::fromStdString(initVal).toDouble());
        ui->cfgTime->setValue(QString::fromStdString(stepTime).toInt());
        ui->cfgValue->setValue(QString::fromStdString(stepVal).toDouble());
    }
    catch(std::logic_error &e)
    {
        QMessageBox::warning(this, "Error", e.what());
    }
}

void InputStepDesigner::applySettings()
{
    try
    {
        string initVal = ui->cfgInitial->text().toStdString();
        string stepTime = ui->cfgTime->text().toStdString();
        string stepVal = ui->cfgValue->text().toStdString();

        m_inputConf->setParameter("init_val", initVal);
        m_inputConf->setParameter("action_time", stepTime);
        m_inputConf->setParameter("action_val", stepVal);
    }
    catch(std::logic_error &e)
    {
        QMessageBox::warning(this, "Error", e.what());
    }
}
